﻿using iChip.Hotspots.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace iChip.Hotspots.Controllers
{
    public class AdminHotspotsController : Controller
    {
        private BO.BOs _bo = new BO.BOs();
        // GET: AdminHotspots
        public ActionResult Index(int? index)
        {
            var model = new List<HotspotsObject>();
            int pageSize = 20;
            int pageIndex = index.GetValueOrDefault();
            if (pageIndex == 0)
            {
                pageIndex = 1;
            }
            model = _bo.Select_Hotspots_Paging(pageIndex, pageSize);

            return View(model);
        }

        [AllowAnonymous]
        public ActionResult AddHotspots(int? Hotspot_id)
        {
            
            var model = new HotspotsModel();
            if (Hotspot_id.GetValueOrDefault() > 0)
            {
                var obj = _bo.Select_Hotspots_by_Id(Hotspot_id.GetValueOrDefault());
                model = new HotspotsModel
                {
                    Hotspot_Address = obj.Hotspot_Address,
                    Hotspot_City = obj.Hotspot_City,
                    Hotspot_Ip = obj.Hotspot_Ip,
                    Hotspot_Location = obj.Hotspot_Location,
                    Hotspot_Mac = obj.Hotspot_Mac,
                    Hotspot_Name = obj.Hotspot_Name,
                    Hotspot_Nasid = obj.Hotspot_Nasid,
                    Hotspot_Region = obj.Hotspot_Region,
                    Hotspot_Status = obj.Hotspot_Status,
                    Hotspot_Type = obj.Hotspot_Type,
                    Hotspot_Id = obj.Hotspot_Id
                };
                ViewBag.State = 1; // cap nhat
            }
            else
            {
                ViewBag.State = 0; // them moi
            }
            return PartialView(model);
        }

        //[HttpPost]
        //[AllowAnonymous]
        //[ValidateAntiForgeryToken]
        //public async Task<ActionResult> AddHotspots(HotspotsModel model)
        //{
        //    if(ModelState.IsValid)
        //    {
        //        return View(model);
        //    }

        //    return null;
        //}

        [HttpPost]
        public JsonResult AddHotspots(HotspotsModel model)
        {
            string mess = string.Empty;
            bool flag = false;
            if (model.Hotspot_Id > 0)
            {
                flag = _bo.Update_Hotspots(model, out mess);
            }
            else
            {
                flag = _bo.Insert_Hotspots(model, out mess);
            }

            return Json(new { Status = flag, Message =  mess});
        }
    }
}